weiXinController.controller('sprOrderPageCtl',['$scope','$state','$stateParams','$timeout','$interval','$compile','$rootScope',function($scope,$state,$stateParams,$timeout,$interval,$compile,$rootScope) {
    console.log({name:$state.current.name,templateUrl:$state.current.templateUrl});

    /**
     * 查询初始化
     */
    $scope.orderSearchObj = {type_account:'',type:'',type_from:'',approval_type_param:''};

    var param = {
        id : 'tableDemo',
        checkbox : false,
        column : [
            {
                type:'rowNum',
                title:'编号',
                titleTdStyle: 'width: 100px;color: #17a2b8;'
            },{
                type:'key',
                mapping:'id',
                show:false
            },{
				mapping: '?',
            	title:'仓库',
            	trans:function(data){
            		return data.name;
            	}
			},{
				mapping: '?',
            	title:'类型',
            	trans:function(data){
            		if(data.type === 'OUT'){
                		return '出库';
                	}
                	if(data.type === 'BORROW'){
                		return '借出';
                	}
            	}
			},{
				mapping: '?',
            	title:'来源',
            	trans:function(data){
            		if(data.type_from === 'IN'){
                		return '自建';
                	}
                	if(data.type_from === 'OUT'){
                		return '同步';
                	}
            	}
			},{
				mapping: '?',
            	title:'审批方式',
            	trans:function(data){
            		if(data.approval_type === 'COMMON'){
                		return '正常';
                	}
                	if(data.type_from === 'URGENT'){
                		return '紧急';
                	}
            	}
			},{
				mapping: '?',
            	title:'审批状态',
            	trans:function(data){
            		 if(data.approval_status === 'INIT'){
                     	return '未提交';
                     }
                     if(data.approval_status === 'CHECKING'){
                    	 return '审批中';
                     }
                     if(data.approval_status === 'PASS'){
                    	 return '通过';
                     }
                     if(data.approval_status === 'REJECT'){
                    	 return '驳回';
                     }
            	}
			},{
				mapping: '?',
            	title:'备件类型',
            	trans:function(data){
            		  if(data.type_account === 'IN'){
                      	return '账内';
                      }
                      if(data.type_account === 'OUT'){
                      	return '账外';
                      }
            	}
			},{
				mapping: '?',
            	title:'状态',
            	trans:function(data){
	        		 if(data.status === 'INIT'){
	                 	return '未完成';
	                 }
	                 if(data.status === 'DOING'){
	                	 return '处理中';
	                 }
	                 if(data.status === 'DONE'){
	                	 return '已完成';
	                 }
	                 if(data.status === 'BINDED'){
	                	 return '已绑定';
	                 }
	                 if(data.status === 'BALANCED'){
	                	 return '已冲销';
	                 }
	                 if(data.status === 'CANCEL'){
	                	 return '已取消';
	                 }
            	}
			},{
                mapping:'?',
                title:'操作时间',
                trans:function(data){
            		return data.gmt_modified ;
                }
			},{
                type: 'function',
                title: '操作',
                titleDivStyle: 'text-align: center',
                dataDivStyle: 'text-align: center',
                func: [{
                    title: '查看',
                    css: 'btn-sm btn-outline-info',
                    text111: '<i class="fas fa-eye"></i>',
                    text: '查看',
                    func: function (data) {
                        $scope.orderView(data.id);
                    }
                },{
                    title: '审批查看',
                    css: 'btn-sm btn-outline-info',
                    text: '审批查看',
                    show: function(data){
                        if(data.approval_status === 'CHECKING'){
                            // return '审批中';
                            return true;
                        }
                        if(data.approval_status === 'PASS'){
                        	// return '通过';
                            return true;
                        }
                        if(data.approval_status === 'REJECT'){
                        	// return '驳回';
                            return true;
                        }
                    },
                    func: function (data) {
                        $scope.showApprovalStatus(data);
                    }
                }]
            }
        ],
		pageConfig:{
			showGoNum:false
		},
        page:function(cmd, currentPage, pageSize, totalPageNum){
            if('first' === cmd){
                currentPage = 1;
            }else if('prev' === cmd){
                currentPage--;
            }else if('next' === cmd){
                currentPage++;
            }else if('last' === cmd){
                currentPage = totalPageNum;
            }else if('change' === cmd){
                currentPage = 1;
            }
            $scope.search(currentPage,pageSize);
        }
    };
    EdeyeAdmin.initDataTablePhone(param, {data:[]});

    /**
     * 搜索
     */
    $scope.search = function(currentPage,pageSize){
        var postData = $scope.orderSearchObj;
        postData.currentPage = (currentPage === undefined ? 1 : currentPage);
        postData.pageSize = (pageSize === undefined ? EdeyeAdmin.getPageSizePhone(param.id) : pageSize);
        postData.approval_type = 'COMMON';
        EdeyeAdmin.post({
            url:'/orderInfo/sprPage',
            data:postData
        },{
            SUCCESS:function(ret){
            	console.log(ret);
                if(ret.code === '200'){
                	if(ret.data != undefined || ret.data != null){
                		EdeyeAdmin.setDataTableDataPhone(param, ret.data);
                	}
                }
            }
        });
    }
    $scope.search();
    
    // 当前审批意见
    $scope.approval_msg = '';
    
    // 历史审批意见
    $scope.history_msg = [];

    // 申领单查看
    $scope.orderView = function(id){
        $scope.approval_msg = '';
        $scope.history_msg = [];
        
        $scope.agreeId = id;
        EdeyeAdmin.post({
            url: '/orderInfo/getSPOrderRecordById',
            data: {
                id:id,
                status:'INPUT'
            }
        }, {
            SUCCESS: function (ret) {
                if(ret.code == 500){
                    EdeyeAdmin.closeMsg(ret.msg,true,2);
                    return;
                }
                $scope.approval_top_id = ret.order.approval_top_id;
                $scope.applicant_user_id = ret.order.applicant_user_id;
                $scope.approval_current_id = ret.order.approval_current_id;
                $scope.orderRecordViewList = ret.data;
                $scope.history_msg = JSON.parse(ret.order.approval_msg);
                $scope.$apply();
                $('#viewStoreModal').modal('show');
            }
        });
    }

    var loginInfo = EdeyeAdmin.getLoginInfo().loginInfo;
    
    // 审批意见驳回或通过,type为sprAgree或sprReject
    $scope.orderApproval = function(type){
    	
    	// 如果是驳回,必须填写审批意见
    	if('sprReject' === type){
            if($scope.approval_msg.length < 1 || $scope.approval_msg.length > 64){
                EdeyeAdmin.closeMsg("请填写审批意见,长度为1～64字符",true,1);
                return;
            }
    	}
    	
    	var param_history_msg = [];
    	for(var tmpKey in $scope.history_msg){
    		param_history_msg.push({
    			id:$scope.history_msg[tmpKey]['id'],
    			name:$scope.history_msg[tmpKey]['name'],
    			msg:$scope.history_msg[tmpKey]['msg'],
				gmt_create:$scope.history_msg[tmpKey]['gmt_create'],
				type:('sprReject' === type ? 'REJECT' : 'AGREE')
    		});
    	}

    	EdeyeAdmin.post({
            url: '/orderInfo/' + type,
            data: {
                id:$scope.agreeId,
                approval_current_id:$scope.approval_current_id,
                approval_top_id: $scope.approval_top_id,
                applicant_user_id:$scope.applicant_user_id,
                approval_msg: {'id':loginInfo.id, 'name': loginInfo.show_name, 'msg': $scope.approval_msg},
                history_msg: param_history_msg
            }
        }, {
            SUCCESS: function (ret) {
            	$('#viewStoreModal').modal('hide');
                $scope.search();
            }
        });
    }

    $scope.test = function () {
        console.info(1);
        $state.go('/spr/order/page', {param:'571987317770682368'});
    }

    // 查看审批状态
	$scope.showApprovalStatus = function(data){
		EdeyeAdmin.post({
			url:'/orderInfo/showApprovalStatus',
			data:{id:data.id}
		},{
			SUCCESS:function(ret){
				if(ret.code === '200'){
					console.log(ret);
					$scope.approvalLevels = ret.approvalLevel;
					$scope.approvalMsg = ret.approvalMsg;
					$scope.$apply();
					$('#viewApprovalModal').modal(true);
				}
			}
		});
	}

    $timeout(function () {
        if($stateParams.param != null){
            $scope.par = $stateParams.param;
            console.info($scope.par);
            $scope.orderView($scope.par);
        }
    },500);

}]);